package com.oracle.lingshangejia.activity.special;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lws.basic.utils.common.AdapterIntegerDefault0;
import com.lws.basic.utils.common.DoubleDefault0Adapter;
import com.oracle.lingshangejia.Constant;
import com.oracle.lingshangejia.R;
import com.oracle.lingshangejia.adapter.special.SpecialDiseaseDetailAdapter;
import com.oracle.lingshangejia.bean.GxyBean;
import com.oracle.lingshangejia.bean.TnbBean;
import com.oracle.lingshangejia.bean.User;
import com.oracle.lingshangejia.model.OkgoNetwork;
import com.oracle.lingshangejia.mvp.base.BaseActivity;
import com.oracle.lingshangejia.utils.MyLogger;
import com.oracle.lingshangejia.utils.MyToast;
import com.oracle.lingshangejia.utils.ObserverOnNext;
import com.oracle.lingshangejia.utils.RxUtils;
import com.tencent.bugly.crashreport.CrashReport;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;

public class TnbDetailActivity extends BaseActivity {

    @BindView(R.id.rv)
    RecyclerView mRv;
    private SpecialDiseaseDetailAdapter mAdapter;
    @Override
    public int setLayoutId() {
        return R.layout.activity_tnb_detail;
    }

    @Override
    public void initData() {
        super.initData();
        ButterKnife.bind(this);
        initRecycelrView();
        getData();
    }

    private void initRecycelrView(){
        mRv.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new SpecialDiseaseDetailAdapter(null);
        mRv.setAdapter(mAdapter);
    }


    private void getData(){
        showLoading();
        Observable.create(new ObservableOnSubscribe<TnbBean>() {
            @Override
            public void subscribe(ObservableEmitter<TnbBean> emitter) throws Exception {
                int id = getIntent().getIntExtra("id", 0);
                Map<Object, Object> map = new HashMap<>();
                map.put("token", User.getToken());
                map.put("mark", 1);
                map.put("zxbId", id);
                JSONObject jsonObject = new JSONObject(map);
                MyLogger.i(jsonObject.toString());
                Gson gson = new GsonBuilder()
                        .registerTypeAdapter(Integer.class, new AdapterIntegerDefault0())
                        .registerTypeAdapter(int.class, new AdapterIntegerDefault0())
                        .registerTypeAdapter(Double.class, new DoubleDefault0Adapter())
                        .registerTypeAdapter(double.class, new DoubleDefault0Adapter())
                        .create();
                String json = OkgoNetwork.post(Constant.tnb_detail,jsonObject.toString());
                MyLogger.i(json);
                emitter.onNext(gson.fromJson(json, TnbBean.class));
            }
        }).compose(RxUtils.io_main()).subscribe(new ObserverOnNext<TnbBean>() {
            @Override
            public void onNext(TnbBean bean) {
                try {

                    dismissLoading();
                    if (bean.getData() != null) {
                        List<String> list = new ArrayList<>();
                        list.add("随访方式：" + sffs(bean.getData().getSffs()));
                        list.add("症状：\n" + zz(bean.getData().getTnbZzList(), bean.getData().getTnbTzQt()));
                        list.add("血压(mmHg):收缩率: " + bean.getData().getTnbTzXyss() + "      舒张率: " + bean.getData().getTnbTzXysz());
                        list.add("体重：" + bean.getData().getTnbTzTz());
                        list.add("体质指数kg/m²: " + bean.getData().getTnbTzTzzs());
                        list.add("足背动脉搏动：" + zbdm(bean.getData().getTnbZbdmbd(), bean.getData().getTnbZbdmbdFx()));
                        list.add("其他：" + bean.getData().getTnbZzQt());
                        list.add("日吸烟量(支)：" + bean.getData().getTnbShfszdRxyl());
                        list.add("日饮酒量(两)：" + bean.getData().getTnbShfszdRyjl());
                        list.add("运动：" + bean.getData().getTnbShfszdZyd() + " 次/周    " + bean.getData().getTnbShfszdCyd() + " 分钟/次");
                        list.add("主食(克/天): " + bean.getData().getTnbShfszdZs());
                        list.add("心理调整： " + xltz(bean.getData().getTnbShfszdXltz()));
                        list.add("遵医行为： " + xltz(bean.getData().getTnbShfszdZyxw()));
                        list.add("空腹血糖值(mmol/L): " + bean.getData().getFzjcKfxtz());
                        list.add("糖化血红蛋白：" + bean.getData().getFzjcThxhdb());
                        list.add("服药依从性：" + fyycx(bean.getData().getTnbFyycx()));
                        list.add("药物不良反应：" + (bean.getData().getTnbYwblfy() == 2 ? "" : "有 " + bean.getData().getTnbZbdmbdFx()));
                        list.add("低血糖反映：" + dxtfy(bean.getData().getTnbDxtfy()));
                        list.add("此次随访分类：" + sffl(bean.getData().getTnbCcsffl()));
                        if (!TextUtils.isEmpty(bean.getData().getTnbYyqkYwmc1())) {
                            list.add("药物名称：" + bean.getData().getTnbYyqkYwmc1() + "\n"
                                    + "     每日次数：" + bean.getData().getTnbYyqkMejc1()
                                    + "     每次几颗：" + bean.getData().getTnbYyqkMcjk1());
                        }
                        if (!TextUtils.isEmpty(bean.getData().getTnbYyqkYwmc2())) {
                            list.add("药物名称：" + bean.getData().getTnbYyqkYwmc2() + "\n"
                                    + "     每日次数：" + bean.getData().getTnbYyqkMejc2()
                                    + "     每次几颗：" + bean.getData().getTnbYyqkMcjk2());
                        }
                        if (!TextUtils.isEmpty(bean.getData().getTnbYyqkYwmc3())) {
                            list.add("药物名称：" + bean.getData().getTnbYyqkYwmc3() + "\n"
                                    + "     每日次数：" + bean.getData().getTnbYyqkMejc3()
                                    + "     每次几颗：" + bean.getData().getTnbYyqkMcjk3());
                        }
                        if (!TextUtils.isEmpty(bean.getData().getTnbYyqkYwmc3())) {
                            list.add("胰岛素种类：" + bean.getData().getTnbYyqkYdszl() + "\n"
                                    + "     用法：" + bean.getData().getTnbYwblfy()
                                    + "     用量：" + bean.getData().getTnbYyqkYdsyl());
                        }

                        if (bean.getData().getZzjy() == 2) {// 1无 2有
                            list.add("转诊原因：" + bean.getData().getZzyy());
                            list.add("转诊机构：" + bean.getData().getZzjg());
                            list.add("转诊科室：" + bean.getData().getZzks());
                        }
                        list.add("时间：" + bean.getData().getCreateDate());
                        mAdapter.setNewData(list);
                    } else {
                        MyToast.show(bean.getMsg());
                    }
                }catch (Exception e){
                    MyToast.show(e.getMessage());
                    CrashReport.postCatchedException(e);
                }
            }
        });

    }

    //此次随访分类
    private String sffl(int index){
        String xl =" ";
        switch (index){
            case 1:
                xl = " 控制满意 ";
                break;
            case 2:
                xl = " 控制不满意 ";
                break;
            case 3:
                xl = " 不良反应 ";
                break;
            case 4:
                xl = " 并发症 ";
                break;
        }
        return xl;
    }

    //低血糖反映
    private String dxtfy(int index){
        String s ="";
        switch (index){
            case 1:
                s ="无";
                break;
            case 2:
                s = "偶尔";
                break;
            case 3:
                s ="频繁";
                break;
        }
        return s;
    }

    //心理调整
    private String xltz(int index){
        String xl =" ";
        switch (index){
            case 1:
                xl = " 良好 ";
                break;
            case 2:
                xl = " 一般 ";
                break;
            case 3:
                xl = " 差 ";
                break;
        }
        return xl;
    }

    //服药依从性
    private String fyycx(int index){
        String xl =" ";
        switch (index){
            case 1:
                xl = " 规律 ";
                break;
            case 2:
                xl = " 间断 ";
                break;
            case 3:
                xl = " 不服药 ";
                break;
        }
        return xl;
    }

    //足背动脉
    private String zbdm(int index ,String xq){
            String s ="";
            switch (index){
                case 1:
                    s = "触及正常";
                    break;
                case 2:
                    s = "减弱  "+xq;
                    break;
                case 3:
                    s = "消失  "+xq;
                    break;
            }
            return s;
    }

    //随访方式
    private String sffs(int index){
        String s = "";
        switch (index){
            case 1:
                s = "门诊";
                break;
            case 2:
                s = "家庭";
                break;
            case 3:
                s = "电话";
                break;

        }
        return s;

    }

    //症状
    private String zz(List<String> list, String qt){
        String zz ="";
        for (int i = 0; i<list.size(); i++){
            switch (list.get(i)){
                case "1":
                    zz += "无症状 ";
                    break;
                case "2":
                    zz += "多饮 ";
                    break;
                case "3":
                    zz+= "多食 ";
                    break;
                case "4":
                    zz += "多尿 ";
                    break;
                case "5":
                    zz += "视力模糊 ";
                    break;
                case "6":
                    zz += "感染 ";
                    break;
                case "7":
                    zz += "手脚麻木 ";
                    break;
                case "8":
                    zz += "下肢浮肿 ";
                    break;
                case "9":
                    zz += "体重明显下降 ";
                    break;
                case "10":
                    zz += qt;
                    break;
            }
        }
        return zz;

    }

}
